package com.question;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DataProcess {
    int data=1;

    Lock lock=new ReentrantLock();
    Condition condition1=lock.newCondition();
    Condition condition2=lock.newCondition();
    Condition condition3=lock.newCondition();

    Object o=new Object();
    int threadId;


    public void  process(int threadId )
    {
        while(data < 75){
            synchronized (o){
                if (data/5%3 + 1 == threadId){
                    //该判断确保对应线程输出
                    for (int i = 0; i <5; i++) {
                        System.out.println("线程"+threadId+":"+(data++));
                    }
                    o.notifyAll();
                }else {
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }

    }
    public  void process1()
    {

        while(data<=75)
        {
            lock.lock();
            if((data%15-1)/5!=0)
            {
                try {
                    condition1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"线程输出——"+data);
            data++;
            condition2.signal();
            lock.unlock();
        }
    }
    public  void process2()
    {
        while(data<=75)
        {
            lock.lock();

            if((data%15-1)/5!=1)
            {
                try {
                    condition1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"线程输出——"+data);
            data++;
            condition3.signal();
            lock.unlock();

        }
    }
    public  void process3()
    {
        while(data<=75)
        {
            lock.lock();

            if((data%15-1)/5!=2)
            {
                try {
                    condition1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"线程输出——"+data);
            data++;
            condition1.signal();
            lock.unlock();

        }
    }
}
